
<template>
  <div class="page-container fullpage">
    <el-card>
      <h3>删除日程</h3>

      <!-- 顶部提示已隐藏 -->

      <el-table :data="tasks" style="width:100%; margin-top:12px">
        <el-table-column prop="masname" label="任务名" />
        <el-table-column prop="massion" label="内容" />
        <el-table-column prop="massiontime" label="时间" />
        <!-- 创建者/最后修改者已隐藏 -->
        <el-table-column label="操作">
          <template #default="{ row }">
            <el-button type="danger" size="small" @click="deleteRow(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div style="margin-top:12px; text-align:right">
        <el-button @click="goBack">返回主界面</el-button>
      </div>

      <center-toast v-model:modelValue="toastVisible" :title="toastTitle" :message="toastMessage" :type="toastType" />
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import api from '../utils/api'
import CenterToast from './CenterToast.vue'

const queryName = ref(localStorage.getItem('username') || '')
const tasks = ref<Array<any>>([])
const serverMessage = ref('')

const toastVisible = ref(false)
const toastTitle = ref('')
const toastMessage = ref('')
const toastType = ref<'info'|'success'|'error'>('info')

const showToast = (type: 'info'|'success'|'error', title: string, message: string) => {
  toastType.value = type
  toastTitle.value = title
  toastMessage.value = message
  toastVisible.value = true
}

const load = async () => {
  const username = localStorage.getItem('username') || ''
  if (!username) { showToast('error', '错误', '未检测到登录用户名，请先登录'); return }
  try {
    const resp: any = await api.get('/stumsg', { params: { username } })
    console.log('get stumsg resp', resp)
    if (resp && resp.operationResult === 'SUCC') {
      tasks.value = resp.data || []
      serverMessage.value = resp.errorMsg || ''
    } else {
      serverMessage.value = resp?.errorMsg || '未找到任务'
      showToast('error', '失败', serverMessage.value)
      tasks.value = []
    }
  } catch (err) {
    console.error(err)
    showToast('error', '请求失败', '获取日程失败')
  }
}

const deleteRow = async (row: any) => {
  const username = localStorage.getItem('username') || ''
  if (!username) { showToast('error', '错误', '未检测到登录用户名，请先登录'); return }
  try {
    const resp: any = await api.delete('/stumsg', { params: { username, name: row.masname } })
    if (resp && resp.operationResult === 'SUCC') {
      showToast('success', '已删除', resp.errorMsg || '删除成功')
      // refresh list
      await load()
    } else {
      showToast('error', '删除失败', resp?.errorMsg || '删除失败')
    }
  } catch (err) {
    console.error(err)
    showToast('error', '请求失败', '删除请求失败')
  }
}

onMounted(() => { if (queryName.value) load() })

const router = useRouter()
const goBack = () => { try { router.push('/dashboard') } catch (e) {} }
</script>

<style scoped>
.page-container { padding: 16px }
</style>

